#include <dos.h>
#include <stdlib.h>
#include <math.h>
#include "user.h"
#include "user_doe.h"

double FI(double a,double b);
double K1(double u,double r1);
double K2(double u,double r2,double r1);
const double H=1/sqrt(5);
const double H1=sqrt(1-H*H);
static double S,h,w,k,fi=M_PI_2-asin(H), wl;
static double foc;
static  double R3,R2,R1,d,Cos,angle;

double leta(double u,double v) {
 if(((u*u+v*v)>=R2*R2)&&((u*u+v*v)<R3*R3))
  return (K2(cos(fi)*u+sin(fi)*v,R3,R2)+d*H*u)*k/foc;
 if(((u*u+v*v)>R1*R1)&&((u*u+v*v)<=R2*R2))
  return (K2(cos(-fi)*u+sin(-fi)*v,R2,R1)-d*H*u)/foc*k;
 if((u*u+v*v)<R1*R1)
  return K1(u,R1)/foc*k;
 return (K2((-(R3+S*1.5))*cos(fi)+sin(fi)*sqrt((R3-S*1.5)*S*1.5),R3,R2)+
	     d*H*(-(R3+S*1.5)))*k/foc;
}
double leta1(double u,double v){
return leta(Cos*u,v);
}

void lettera()   {
   char *InputValues[5];
   char *Title={" Focusator at Letter A"};

   char *ParNames[]={
		     "Hight              :",
		     "Width              :",
		     "Wavelength         :",
		     "Discretization Step:",
		     "Angle              :",
		     "Focal length       :",
		     "Focal Horiz.   Size:"
		     };
   char *Default[]={
		    "    12.8        ",
		    "    12.8        ",
		    "    1.06        ",
		    "     .1         ",
		    "    0.0         ",
		    "   500.0        ",
		    "    2.0          "
		    };
   if(!sedit(25,5,7,ParNames,InputValues,Default,Title))  return ;

   // read input data
   double s;
   double a,b;
   sscanf(InputValues[0],"%lf \0",&h);
   sscanf(InputValues[1],"%lf \0",&w);
   sscanf(InputValues[2],"%lf \0",&wl);
   sscanf(InputValues[3],"%lf \0",&s);
   sscanf(InputValues[4],"%lf \0",&angle);
   Cos=cos(angle*M_PI/180);
   sscanf(InputValues[5],"%lf \0",&a);
   sscanf(InputValues[6],"%lf \0",&b);
   foc=a;
   d=b;
   S=s;
   R3=h/2;
   R2=R3*sqrt((H+1)/(2+H));
   R1=R3*sqrt(H/(2+H));
   k=2*M_PI/(wl*0.001);

   // max&min values of phase in this mask
   //double max=B2;
   double max=leta(R1-S*1.5,0);
   double min=leta(-(R3-S*1.5),sqrt((R3-S*1.5)*S*1.5));
   NonSymmetricDOE(Phase, max, min, leta1);

   // control the DOE sizes and output they to InfoWindow
   // if sizes wrong (very big) - return
   UndoMenu(2) ;   //  delete 2 last level submenu on the screen
   if(MessageSize(w/Cos, h, s)) return;
   // Output information to InfoWindow
   MessageName("Focus-or at Letter A ");  // write title
   MessageParam(1," Focusator at Letter  A ");
   MessageParam(2,"Hight  %f",h);
   MessageParam(3,"Width  %f",w);
   MessageParam(4,"Wavelength %f",wl);
   MessageParam(5,"Step   %f",s);
   MessageParam(6,"Angle   %f",angle);
   MessageParam(7,"Foc  = %f",a);
   MessageParam(8,"High = %f",b);
   MessageOut();                  // output all string to InfoWindow
   return;
}
double FI(double u,double r1)
{
  if(fabs(u)<=r1) return r1*r1*( u*asin(u/r1)+sqrt(r1*r1-u*u) )
			-pow((r1*r1-u*u),3/2)/3+M_PI_2*r1*r1*u;
  if(u>r1) return M_PI*r1*r1*u;
  return 0.;
};
double K2(double u,double r2,double r1)
{ double C2=d/M_PI_2/(r2*r2-r1*r1);
  double D2=d*r1*r1/(r2*r2-r1*r1);
  return C2*(
    r2*r2*( u*asin(u/r2)-M_PI_2*r2+sqrt(r2*r2-u*u) )-pow((r2*r2-u*u),3/2)/3
    -FI(u,r1)
	    )
	    +D2*(u+r2);
};
double K1(double u,double r1)
{ double C=d*sqrt(H1)/M_PI/r1/r1;
  return C*(r1*r1*(u*asin(u/r1)+sqrt(r1*r1-u*u)+r1*M_PI_2)-
	    pow((r1*r1-u*u),3/2)/3);
}